package com.xidian.zhiancloud.controller.dangerWorkTicket.workManager.WorkTicket;

import com.xidian.zhiancloud.controller.base.BaseController;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.model.dangerWorkTicket.TemplateSource;
import com.xidian.zhiancloud.service.dangerWorkTicket.WorkTicket.TemplateSourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/TemplateSource")
public class TemplateSourceController extends BaseController {

    @Autowired
    private TemplateSourceService templateSourceService;

    /**
     * 初始化作业票模板，新公司新加九条作业票模板记录。整个过程只能初始化一次。
     *
     * @return
     */
    @GetMapping("/init")
    public ResultDTO<Boolean> init() {
        Boolean res = templateSourceService.templateSourceInit();
        return ResultDTO.successOf(res, GeneralResponseEnums.SUCCESS);
    }

    /**
     * 列表查找作业票模板源
     *
     * @return 所有作业票模板源
     */
    @GetMapping("/getList")
    public ResultDTO<List<TemplateSource>> getList() {
        List<TemplateSource> list = templateSourceService.list();
        if (list == null) {
            return ResultDTO.errorOf(GeneralResponseEnums.GET_FAILED);
        }
        return ResultDTO.successOf(list, GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id查找作业票模板源
     *
     * @param id
     * @return 作业票模板源
     */
    @GetMapping("/getById")
    public ResultDTO<TemplateSource> getById(@RequestParam("id") Long id) {
        TemplateSource templateSource = templateSourceService.getById(id);
        if(templateSource == null){
            return ResultDTO.errorOf(GeneralResponseEnums.GET_FAILED);
        }
        return ResultDTO.successOf(templateSource, GeneralResponseEnums.SUCCESS);
    }
}
